// --- Copyright (c) 2006-2008 Stefan Kyntchev ---
// This software is written and copyrighted by Stefan Kyntchev 
// and BeyondPod Team members. All rights are reserved.
// Author contact: support@beyondpod.mobi
// ------------------------------------------------------------------------
// This file is part of BeyondPod RSS Feed Reader and Podcast manager
// (www.codeplex.com/BeyondPod) 
// BeyondPod is free software: you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
//  
// BeyondPod is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more details. 
//  
// You should have received a copy of the GNU General Public License
// along with BeyondPod. If not, see <http://www.gnu.org/licenses/>
// ------------------------------------------------------------------------
// COPYRIGHT NOTICE: This copyright notice may NOT be removed, obscured or modified 
// without written consent from the author.
// --- End copyright notice --- 


using System;
using System.Collections.Generic;
using System.Reflection;

namespace svs.Mobile.BeyondMedia.FeedCore.Helpers
{
	/// <summary>
	/// This class is used to compare any 
	/// type(property) of a class for sorting.
	/// This class automatically fetches the 
	/// type of the property and compares.
	/// </summary>
	public sealed class GenericComparer<T> : IComparer<T>
	{
		public enum SortOrder
		{
			Ascending,
			Descending
		} ;

		#region Member variables

		private readonly string m_sortColumn;
		private readonly SortOrder m_sortingOrder;
		private readonly string[] m_Path;

		#endregion

		#region constructor

		public GenericComparer(string sortColumn, SortOrder sortingOrder)
		{
			char[] dels = {'.'};
			m_sortColumn = sortColumn;
			m_sortingOrder = sortingOrder;
			m_Path = SortColumn.Split(dels);
		}

		#endregion

		#region Public properties

		/// <summary>
		/// Column Name(public property of the class) to be sorted.
		/// </summary>
		public string SortColumn
		{
			get { return m_sortColumn; }
		}


		/// <summary>
		/// Sorting order.
		/// </summary>
		public SortOrder SortingOrder
		{
			get { return m_sortingOrder; }
		}

		#endregion

		#region Public Methods

		/// <summary>
		/// Compare interface implementation
		/// </summary>
		/// <param name="x">custom Object</param>
		/// <param name="y">custom Object</param>
		/// <returns>int</returns>
		public int Compare(T x, T y)
		{
			IComparable obj1 = GetObjectValue(x);
			IComparable obj2 = GetObjectValue(y);

			if (obj1 == null && obj2 == null)
				return 0;

			if (m_sortingOrder == SortOrder.Ascending)
			{
				if (obj1 == null)
					return -1;

				return (obj1.CompareTo(obj2));
			}

			else
			{
				if (obj2 == null)
					return -1;

				return (obj2.CompareTo(obj1));
			}
		}

		private IComparable GetObjectValue(Object i_start)
		{
			return (IComparable) GetValue(0, i_start);
		}

		private object GetValue(int i_level, object i_start)
		{
			PropertyInfo propertyInfo = i_start.GetType().GetProperty(m_Path[i_level]);
			object value = propertyInfo.GetValue(i_start, null);

			if (i_level < m_Path.Length - 1)
				return GetValue(i_level + 1, value);
			else
				return value;
		}

		#endregion
	}
}